package com.example.reagent_management.ui.operatedialog.dialogs

import android.view.View
import androidx.databinding.library.baseAdapters.BR
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.example.lib_base.adapter.ItemClickListener
import com.example.lib_base.base.BaseDBDialogFragment
import com.example.lib_base.base.DataBindingConfig
import com.example.lib_base.utils.onQueryTextFlow
import com.example.reagent_management.AppEvent
import com.example.reagent_management.R
import com.example.reagent_management.databinding.FragmentInStoreChooseBinding
import com.example.reagent_management.room.entity.InStoreChoose
import com.example.reagent_management.ui.operatedialog.adapter.InStoreChooseAdapter
import com.example.reagent_management.ui.operatedialog.viewmodle.StoreViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

/*
* 根据试剂信息选择入库的信息
*
* */
@AndroidEntryPoint
class InStoreChooseFragment : BaseDBDialogFragment() {
    private val mState: StoreViewModel by viewModels({ requireParentFragment() })
    private lateinit var mEvent: AppEvent
    private lateinit var job: Job
    private var filterId = 0L

    override fun getDataBindingConfig(): DataBindingConfig? {
        mEvent = getApplicationScopeViewModel(AppEvent::class.java)
        val adapter = InStoreChooseAdapter()
        setOnDataListener { any ->
            if (any is Long) {
                filterId = any
                job = mState.batchList(any, "")
                mState.batches.observe(viewLifecycleOwner, {
                    adapter.submitData(lifecycle, it)
                })
            }
        }

        adapter.setOnItemClick(object : ItemClickListener {
            override fun itemClick(data: Any?) {
                (data as InStoreChoose).let {
                    mEvent.setInStoreChoose(data)
                    dismissAllowingStateLoss()
                }
            }
        })
        return DataBindingConfig(R.layout.fragment_in_store_choose, BR.VM, mState)
            .addBindingParam(BR.adapter, adapter)
    }

    override fun initViewOrObserve() {
        setTransAndHeight(true, 0.5f, true)
        (getBinding() as FragmentInStoreChooseBinding).apply {
            no.setOnClickListener { dismissAllowingStateLoss() }
            search.setOnSearchClickListener { title.visibility = View.INVISIBLE }
            search.setOnCloseListener {
                title.visibility = View.VISIBLE
                false
            }
            lifecycleScope.launch {
                search.onQueryTextFlow()
                    .debounce(500)
                    .distinctUntilChanged()
                    .flowOn(Dispatchers.IO)
                    .collect {
                        if (job.isActive) job.cancel()
                        if (job.isCancelled) {
                            job = mState.batchList(filterId, it)
                        }
                    }
            }
        }
    }
}